package com.wwm.springcloud.userservice.common;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.Map;

public class JacksonUtil {
    private static ObjectMapper mapper = new ObjectMapper();

    private JacksonUtil() {
    }

    public static ObjectMapper getMapper() {
        return mapper;
    }

    public static <T> T jsonStrToObject(String jsonStr, Class<T> valueType) {
        try {
            return mapper.readValue(jsonStr, valueType);
        } catch (IOException var3) {
            return null;
        }
    }

    public static <T> T ObjectNodeToObject(ObjectNode objNode, Class<T> valueType) {
        try {
            return mapper.treeToValue(objNode, valueType);
        } catch (JsonProcessingException var3) {
            return null;
        }
    }

    public static <T> String beanToString(T input) {
        try {
            return mapper.writeValueAsString(input);
        } catch (JsonProcessingException var2) {
            return null;
        }
    }

    public static <T> Map beanToMap(T input) {
        String tmp = beanToString(input);
        return null != tmp ? (Map)jsonStrToObject(tmp, Map.class) : null;
    }

    static {
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}